<?php
namespace Admin\Controller;

class SysModuleController extends BaseController {
	public function index()
	{
        $this->addtitle = L('ADD_TITLE');
        $this->ok = L('BUTTON_OK');
        $this->cancel = L('BUTTON_CANCEL');
        $this->setLangtable('sys_module');

		$this->display();
	}

    public function getModule()
    {
        $data = $this->getChilds(-1);
        $this->ajaxReturn($data, 'json');
    }

    protected function getChilds($pid)
    {
        $data = array();
        $lang = \Common\Common\WebAction::getLangField();
        $sql = 'select `parentid`,`moduleid`,`moduletype`,`icon`,IFNULL(`' . $lang . '`,modulename) as modulename'
            . ' from sys_module where `parentid`=' . $pid . ' order by sort';
        $module = M();
        $childs = $module->query($sql);

        if(count($childs)>0){
            foreach ($childs as $child) {
                $data_child = array();
                $data_child[id] = $child[moduleid];
                $data_child[text] = $child[modulename];
                $data_child[attributes] = array('moduletype' => $child[moduletype]);
                if($child[moduletype]==0){
                    $data_child[children] = $this->getChilds($child[moduleid]);
                };
                array_push($data, $data_child);
            }
        }

        return $data;
    }

    public function getCurrentModule($mid)
    {
        $module = M('SysModule');
        $data = $module->where('parentid='.$mid)->order('sort')->select();
        if(count($data)==0){
        	$data = $module->where('moduleid='.$mid)->select();
        }
        $this->ajaxReturn($data, 'json');
    }

    public function getModuleType()
    {
        $data = array(
            array('id' => '1', 'text' => 'Page'),
            array('id' => '0', 'text' => 'Folder')
        );
        $this->ajaxReturn($data, 'json');
    }

    public function getTarget()
    {
        $data = array(
            array('id' => '_self', 'text' => '_self'),
            array('id' => '_blank', 'text' => '_blank'),
            array('id' => '_top', 'text' => '_top'),
            array('id' => '_content', 'text' => '_content')
        );
        $this->ajaxReturn($data, 'json');
    }

    public function updateModule()
    {
        $module = M('SysModule');
        $data = json_decode($_POST[updated]);
        foreach ($data as $value) {
            while (list($key, $val) = each($value)) {
                $updated[$key] = $val;
            }
            $module->save($updated);
        }
        
        $result['statuscode'] = 0;
        $result['statusmessage'] = L('EDT_TIP');
        $this->ajaxReturn($result, 'json');
    }

    public function saveModule()
    {
        $module = M('SysModule');
        $data[parentid] = $_POST[afld_SysModule_parentid];
        $data[moduleid] = $_POST[afld_SysModule_moduleid];
        $data[modulename] = $_POST[afld_SysModule_modulename];
        $data[moduletype] = $_POST[afld_SysModule_moduletype];
        $data[url] = $_POST[afld_SysModule_url];
        $data[target] = $_POST[afld_SysModule_target];
        $data[icon] = $_POST[afld_SysModule_icon];
        $data[sort] = $_POST[afld_SysModule_sort];
        $data[enus] = $_POST[afld_SysModule_enus];
        $data[zhcn] = $_POST[afld_SysModule_zhcn];
        $data[zhtw] = $_POST[afld_SysModule_zhtw];
        $module->add($data);

        $result['statuscode'] = 0;
        $result['statusmessage'] = 'OK';
        $this->ajaxReturn($result, 'json');
    }

    public function editButton()
    {
        $this->buttonlist = L('BUTTON_LIST');
        parent::setLangtable('vsys_pagebutton');
        $this->display();
    }

    public function getButtonlist($mid, $type)
    {
        $sql = "select btn_id as id,btn_name as text from sys_button where btn_id ";
        if($type == 'all') $sql.= " not ";        
        $sql.= "in (select buttonid from sys_pagebutton where moduleid='$mid')";
        $module = M();
        $data = $module->query($sql);
        $this->ajaxReturn($data, 'json');
    }

    public function getPageButtonlist($mid)
    {
        $sql = "select b.id, a.btn_id,a.btn_name,b.handler,b.sort from sys_button a, sys_pagebutton b ".
            " where a.btn_id=b.buttonid and b.moduleid='$mid' order by b.sort";
        $module = M();
        $data = $module->query($sql);
        $this->ajaxReturn($data, 'json');
    }

    public function updatePageButton()
    {
        $module = M('SysPagebutton');

        if(isset($_POST[inserted])){
            $inserted = json_decode($_POST[inserted],true);
            if(count($inserted)>0) $this->addPageButton($module, $inserted);
        }
        if(isset($_POST[updated])){
            $updated = json_decode($_POST[updated],true);
            if(count($updated)>0) $this->edtPageButton($module, $updated);
        }
        if(isset($_POST[deleted])){
            $deleted = json_decode($_POST[deleted],true);
            if(count($deleted)>0) $this->delPageButton($module, $deleted);
        }
                
        $result['statuscode'] = 0;
        $result['statusmessage'] = L('SAVE_TIP');
        $this->ajaxReturn($result, 'json');
    }

    protected function addPageButton($module, $data)
    {
        $moduleid = $_GET[mid];
        foreach ($data as $value) {
            $inserted[moduleid] = $moduleid;
            $inserted[buttonid] = $value[btn_id];
            $inserted[handler] = $value[handler];
            $inserted[sort] = $value[sort];
            $module->add($inserted);
        }
    }

    protected function edtPageButton($module, $data)
    {
        $moduleid = $_GET[mid];
        foreach ($data as $value) {
            $updated[moduleid] = $moduleid;
            $updated[buttonid] = $value[btn_id];
            $updated[handler] = $value[handler];
            $updated[sort] = $value[sort];
            $module->save($updated);
        }
    }

    protected function delPageButton($module, $data)
    {
        $moduleid = $_GET[mid];
        foreach ($data as $value) {
            $module->where("id=$value[id]")->delete();
        }
    }
}